package com.cskaoyan.service.wx_address;

import com.cskaoyan.bean.bo.wx_address.AddressSaveBO;
import com.cskaoyan.bean.po.ohter.MarketAddress;
import com.cskaoyan.bean.po.ohter.MarketAddressExample;
import com.cskaoyan.bean.po.wx_address.AddressInfo;
import com.cskaoyan.bean.vo.wx_address.AddressDetailVO;
import com.cskaoyan.bean.vo.wx_address.AddressListVO;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: market
 * @description: 用户管理地址实现，注册组件
 * @author: zhong
 * @create: 2022-07-21 21:02
 **/
@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public AddressListVO listAddressOp(Integer userId) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<AddressInfo> addressInfos = marketAddressMapper
                .selectByExample(example)
                .stream()
                .map(address -> {
                    AddressInfo addressInfo = new AddressInfo();
                    BeanUtil.copyBean(address, addressInfo);
                    return addressInfo;
                })
                .collect(Collectors.toList());
        int limit = addressInfos.size();
        Long total = (long) limit;
        return new AddressListVO(limit,1,1,total,addressInfos);
    }

    @Override
    public Integer addAddressOp(AddressSaveBO addressInfo, Integer userId) {
        if (addressInfo.getIsDefault()) {
            //设为唯一默认，将同一用户的其他isDefault更新为false
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andIsDefaultEqualTo(true);
            MarketAddress changeTarget = new MarketAddress();
            changeTarget.setIsDefault(false);
            marketAddressMapper.updateByExampleSelective(changeTarget, example);
        }

        MarketAddress newAddress = new MarketAddress();
        BeanUtil.copyBean(addressInfo,newAddress);
        newAddress.setId(null);
        newAddress.setUserId(userId);
        newAddress.setAddTime(new Date());
        newAddress.setUpdateTime(newAddress.getAddTime());
        int insertRow = marketAddressMapper.insertSelective(newAddress);
        return newAddress.getId();
    }

    @Override
    public Integer updateAddressOp(AddressSaveBO addressInfo, Integer userId) {
        if (addressInfo.getIsDefault()) {
            //设为唯一默认，将同一用户的其他isDefault更新为false
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andIsDefaultEqualTo(true);
            MarketAddress changeTarget = new MarketAddress();
            changeTarget.setIsDefault(false);
            marketAddressMapper.updateByExampleSelective(changeTarget, example);
        }

        MarketAddress targetAddress = new MarketAddress();
        BeanUtil.copyBean(addressInfo, targetAddress);
        int updateRow = marketAddressMapper.updateByPrimaryKeySelective(targetAddress);
        return targetAddress.getId();
    }

    @Override
    public AddressDetailVO getDetailOp(int id) {
        MarketAddress addressInfo = marketAddressMapper.selectByPrimaryKey(id);
        AddressDetailVO detailVO = new AddressDetailVO();
        BeanUtil.copyBean(addressInfo, detailVO);
        return detailVO;
    }

    @Override
    public int delAddressOp(int id) {
        MarketAddress targetAddress = new MarketAddress();
        targetAddress.setId(id);
        targetAddress.setDeleted(false);
        int updateRow = marketAddressMapper.updateByPrimaryKeySelective(targetAddress);
        if (updateRow == 0) {
            return Constant.NOT_FOUND_CODE;
        }
        return Constant.OK_CODE;
    }


}
